package com.education.platform.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import com.education.platform.common.Result;
import com.education.platform.dto.order.AdminOrderQueryDTO;
import com.education.platform.dto.order.CreateOrderDTO;
import com.education.platform.service.OrderService;
import com.education.platform.vo.order.OrderVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Tag(name = "订单管理", description = "订单创建、查询等接口")
@RestController
@RequestMapping("/api/order")
@RequiredArgsConstructor
@SaCheckLogin
public class OrderController {

    private final OrderService orderService;

    @Operation(summary = "创建订单（预约教员）")
    @PostMapping("/create")
    public Result<OrderVO> createOrder(@Validated @RequestBody CreateOrderDTO createOrderDTO) {
        OrderVO order = orderService.createOrder(createOrderDTO);
        return Result.success(order);
    }

    @Operation(summary = "获取我的订单列表")
    @GetMapping("/my-list")
    public Result<Map<String, Object>> getMyOrders(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String status
    ) {
        Map<String, Object> result = orderService.getMyOrders(page, size, status);
        return Result.success(result);
    }

    @Operation(summary = "获取订单详情")
    @GetMapping("/{id}")
    public Result<OrderVO> getOrderDetail(@PathVariable Long id) {
        OrderVO order = orderService.getOrderDetail(id);
        return Result.success(order);
    }

    @Operation(summary = "取消订单")
    @PostMapping("/{id}/cancel")
    public Result<Void> cancelOrder(@PathVariable Long id, @RequestParam String reason) {
        orderService.cancelOrder(id, reason);
        return Result.success();
    }

    @Operation(summary = "支付订单")
    @PostMapping("/{id}/pay")
    public Result<OrderVO> payOrder(@PathVariable Long id) {
        OrderVO order = orderService.payOrder(id);
        return Result.success(order);
    }

    // ========== 管理员接口 ==========

    @Operation(summary = "管理员查询所有订单")
    @GetMapping("/admin/list")
    public Result<Map<String, Object>> adminGetOrders(AdminOrderQueryDTO queryDTO) {
        Map<String, Object> result = orderService.adminGetOrders(queryDTO);
        return Result.success(result);
    }

    @Operation(summary = "管理员更新订单状态")
    @PostMapping("/admin/{id}/status")
    public Result<Void> adminUpdateOrderStatus(
            @PathVariable Long id,
            @RequestParam String status,
            @RequestParam(required = false) String remark
    ) {
        orderService.adminUpdateOrderStatus(id, status, remark);
        return Result.success();
    }

    @Operation(summary = "管理员删除订单")
    @DeleteMapping("/admin/{id}")
    public Result<Void> adminDeleteOrder(@PathVariable Long id) {
        orderService.adminDeleteOrder(id);
        return Result.success();
    }
}

